
/**
 * Project Name:cats-core-impl
 * File Name:RoleServiceImpl.java
 * Package Name:com.catsic.core.role.service.impl
 * Date:2014年2月27日下午1:36:38
 * Copyright (c) 2014, oa.catsic.com All Rights Reserved.
 */
 
package com.catsic.core.role.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.catsic.base.BaseServiceImpl;
import com.catsic.base.exception.CatsicException;
import com.catsic.core.conditon.RoleCondition;
import com.catsic.core.constants.Constants;
import com.catsic.core.convert.Converts;
import com.catsic.core.entity.Menu;
import com.catsic.core.entity.Organ;
import com.catsic.core.entity.Resource;
import com.catsic.core.entity.Role;
import com.catsic.core.entity.User;
import com.catsic.core.exception.CatsicExceptionCode;
import com.catsic.core.form.MenuForm;
import com.catsic.core.form.OrganForm;
import com.catsic.core.form.ResourceForm;
import com.catsic.core.form.RoleForm;
import com.catsic.core.menu.service.MenuService;
import com.catsic.core.organ.service.OrganService;
import com.catsic.core.resource.service.ResourceService;
import com.catsic.core.role.dao.RoleDao;
import com.catsic.core.role.service.RoleService;
import com.catsic.core.specification.RoleSpec;
import com.catsic.core.user.service.UserService;

/**
 * ClassName: RoleServiceImpl
 * Description: 角色Service实现类
 * date: 2014年2月27日 下午1:36:38
 * author: yangyd
 */
@Service("roleService")
public class RoleServiceImpl extends BaseServiceImpl<Role, String> 
		implements	RoleService{

	@Autowired
	private RoleDao roleDao;
	
	@Autowired
	private OrganService organService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private MenuService menuService;
	
	@Autowired
	private ResourceService resourceService;
	
	/**
	 * @see com.catsic.core.base.BaseServiceImpl#add(java.lang.Object)
	 */
	@Override
	public RoleForm add(RoleForm form){
		Organ organ = organService.findById(form.getOrganId());
		Role role = new Role();
		role.setName(form.getName());
		role.setOrgan(organ);
		role.setResources(resourceService.findByLikeCode("000%"));
		return Converts.convert(super.add(role));
	}
	
	/**
	 * @see com.catsic.core.role.service.RoleService#update(java.lang.Long, com.catsic.core.entity.Role)
	 */
	@Override
	public RoleForm update(RoleForm form) {
		Role role = findById(form.getId());
		role.setName(form.getName());
		return Converts.convert(super.add(role));
	}
	
	/**
	 * @see com.catsic.core.role.service.RoleService#delete(java.lang.Long)
	 */
	@Override
	public void delete(String id){
		Role role = findById(id);
		
		if(!CollectionUtils.isEmpty(role.getUsers())){
			throw new CatsicException(CatsicExceptionCode.角色管理_角色已被授权给用户_无法删除);
		}
		
/*		if(!CollectionUtils.isEmpty(role.getOrgans())){
			throw new CatsicException(CatsicExceptionCode.角色管理_角色已被授权给机构_无法删除);
		}*/
		
		super.delete(role);
	}
	
	/**
	 * @see com.catsic.core.role.service.RoleService#findById(java.lang.String)
	 **/
	@Override
	public Role findById(String id){
		Role role = getById(id);
		if(null == role){
			throw new CatsicException(CatsicExceptionCode.实体不存在,Constants.ROLE);
		}
		return role;
	}
	
	/**
	 * @see com.catsic.core.role.service.RoleService#findRoleById(java.lang.String)
	 **/
	@Override
	public RoleForm findRoleById(String id){
		Role role = getById(id);
		if(null == role){
			throw new CatsicException(CatsicExceptionCode.实体不存在,Constants.ROLE);
		}
		return Converts.convert(role);
	}
	
	/**
	 * @see com.catsic.core.role.service.RoleService#findAll(com.catsic.core.conditon.RoleCondition, org.springframework.data.domain.Pageable)
	 */
	public Page<RoleForm> findAll(RoleCondition condition, Pageable pageable){
		RoleSpec spec = new RoleSpec(condition);
		Page<Role> page = roleDao.findAll(spec, pageable);
		List<RoleForm> listForm = new ArrayList<RoleForm>();
	    for(Role role :page.getContent()){
			listForm.add(Converts.convert(role));
	    }
	    Page<RoleForm> page_ = new PageImpl<RoleForm>(listForm,pageable,page.getTotalElements());
	    return page_;
	}
	
	/**
	 * @see com.catsic.core.role.service.RoleService#findMenusById(java.lang.String)
	 */
	@Override
	public List<MenuForm> findMenusById(String id){
		Role role = findById(id);
		List<MenuForm> listForm = new ArrayList<MenuForm>();
		for(Menu menu : role.getMenus()){
			listForm.add(Converts.convert(menu));
		}
		
		return listForm;
	}
	
	/**
	 * @see com.catsic.core.role.service.RoleService#findResourcesById(java.lang.String)
	 */
	@Override
	public List<ResourceForm> findResourcesById(String id){
		Role role = findById(id);
		List<ResourceForm> listForm = new ArrayList<ResourceForm>();
		
		for(Resource resource : role.getResources()){
			listForm.add(Converts.convert(resource));
		}
		return listForm;
	}
 
	/**
	 * @see com.catsic.core.role.service.RoleService#bind(java.lang.Long, java.util.Set, java.util.Set)
	 */
	public Role bind(Role info){
		Role role = findById(info.getId());
		
		if(CollectionUtils.isEmpty(info.getMenus())){
			throw new CatsicException(CatsicExceptionCode.角色管理_菜单不能为空);
		}
		
		if(CollectionUtils.isEmpty(info.getResources())){
			throw new CatsicException(CatsicExceptionCode.角色管理_权限不能为空);
		}
		
		role.setMenus(info.getMenus());
		role.setResources(info.getResources());
		return super.add(role);
	}
	
	/**
	 * @see com.catsic.core.role.service.RoleService#bindOrgan(java.lang.Object)
	 */
	public Role bindOrgan(Role info){
		Role role = findById(info.getId());
		
		if(CollectionUtils.isEmpty(info.getOrgans())){
			throw new CatsicException(CatsicExceptionCode.角色管理_机构不能为空);
		}
		
		role.setOrgans(info.getOrgans());
		return super.add(role);
	}
	
	/**
	 * @see com.catsic.core.role.service.RoleService#findByOrganId(java.lang.String)
	 */
	@Override
	public List<RoleForm> findByOrganId(String organId){
		List<Role> list = roleDao.findByOrganId(organId);
		List<RoleForm> listForm = new ArrayList<RoleForm>();
		for(Role role:list){
			listForm.add(Converts.convert(role));
		}
		return listForm;
	}
	
	/**
	 * @see com.catsic.core.role.service.RoleService#findByUserId(java.lang.String)
	 */
	@Override
	public List<RoleForm> findByUserId(String userId){
		List<Role> list = roleDao.findByUserId(userId);
		List<RoleForm> listForm = new ArrayList<RoleForm>();
		for(Role role:list){
			listForm.add(Converts.convert(role));
		}
		return listForm;
	}
	
	/**
	 * @see com.catsic.core.role.service.RoleService#getBindRole(java.lang.String)
	 */
	@Override
	public List<RoleForm> getBindRole(String userId){
		User user = userService.findById(userId);
		/**
		 * 超级管理员
		 */
		if(StringUtils.equals(user.getOrgan().getId(), Constants.ENTITY_ID)){
			return findByOrganId(userId);
		}
		/**
		 * 普通管理员
		 */
		return findByUserId(userId);
	}

	/**
	 * @see com.catsic.core.role.service.RoleService#findOrganById(java.lang.String)
	 */
	public List<OrganForm> findOrganById(String id){
		List<Organ> list = roleDao.findOrganById(id);
		List<OrganForm> listForm = new ArrayList<OrganForm>();
		for(Organ organ:list){
			listForm.add(Converts.convert(organ));
		}
		return listForm;
	}
	
	/**
	 * @see com.catsic.core.role.service.RoleService#findRoleByResourceId()
	 */
	public List<Role> findRoleByResourceId(String resourceId){
		return roleDao.findRoleByResourceId(resourceId);
	}
	
	/**
	 * @see com.catsic.core.role.service.RoleService#findRoleByMenuId()
	 */
	public List<Role> findRoleByMenuId(String menuId){
		return roleDao.findRoleByMenuId(menuId);
	}
	
	/**
	 * @see com.catsic.core.role.service.RoleService#findAll()
	 */
	public List<Role> findAll(){
		return  roleDao.findAllRole();
	}
}
